# -*- coding: utf-8 -*-

from django import forms
from helpdesk.models import Categoria, OcorrenciaStatus
from comum.models import Setor
from django.http import HttpResponse
from geraldo.generators import PDFGenerator

#########
# FORMS #
#########

# OCORRENCIA #

class RelatorioOcorrenciaForm(forms.Form):
    
    fieldsets = [
                 (None, {'fields': ['status', 'categoria', 'setor', 'numero_patrimonio', 'agrupamento']})
                 ]
    
    AGRUPAMENTO = (
            ('', u'Nenhum'),
            ('status', u'Status'),
            ('categoria', u'Categoria'),
            ('setor', u'Setor'),
            )
    
    status = forms.ModelChoiceField(label=u'Status', queryset=OcorrenciaStatus.objects.all(), empty_label="Todos", required=False)
    categoria = forms.ModelChoiceField(label=u'Categoria', queryset=Categoria.objects.all(), empty_label="Todos", required=False)
    setor = forms.ModelChoiceField(label=u'Setor', queryset=Setor.objects.all(), empty_label="Todos", required=False)
    numero_patrimonio = forms.CharField(label=u'Número', required=False)
    agrupamento = forms.ChoiceField(label=u'Agrupar por', widget=forms.Select(), choices=AGRUPAMENTO, required=False)
    
    METHOD = 'GET'
    TITLE = 'Gerar Relatório de Ocorrências'
    
#########
# VIEWS #
#########


def OcorrenciaView(request, ocorrencias):
    
    titulo = 'Rélatorio de Ocorrências'
    
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=relatorio.pdf'
    
    report = OcorrenciaReport(queryset=ocorrencias)
    report.generate_by(PDFGenerator, filename=response, variables={'titulo': titulo})

    return response